import http from '@/support/http'
import { ref, unref, watch } from 'vue'
import { Code } from '@/enum/app'
import Message from '@/support/message'
import { FormInstance } from 'element-plus'
import { AxiosResponse } from 'axios'
import { isFunction } from '@/support/helper'

// get table list
export function useCreate(path: string, id: string | number | null = null, _formData: object = {}) {
  const formData = ref<object>(_formData)
  interface Address {
    province: string;
    city: string;
    area: string;
    status: string;
    age: string;
    }

    const addressData = ref<Address>({
        province: '',
        city: '',
        area: '',
        status: '2',
        age: ''
    });
  const isSet = ref<boolean>(false)
  const loading = ref<boolean>()
  const isClose = ref<boolean>(false)
    // 地址拦截
      // 假设你想要访问 formData 中的 id 属性
      let package_id = formData.value.hasOwnProperty('id') ? formData.value.id : id;

      // 使用模板字符串构建 URL，确保安全地处理参数
      let url = `intercept/intercept?page=1&limit=10&package_id=${package_id}`;
  
      // 发起网络请求
      http.get(url).then(res => {
          // 处理响应
          if(res.data.data.length > 0){
            addressData.value = res.data.data[0];
            isSet.value = true
            // console.log(addressData)
          }else{
            isSet.value = false
          }
      }).catch(error => {
          // 处理错误
      });
 


  // 创建前 hook
  const beforeCreate = ref()
  // 更新前 hook
  const beforeUpdate = ref()

  const afterCreate = ref()
  const afterUpdate = ref()
  // store
  function store(path: string, id: string | number | null = null) {
    
    loading.value = true
    let promise: Promise<AxiosResponse> | null = null
    if (id) {
      if (isFunction(beforeUpdate.value)) {
        beforeUpdate.value()
      }
      
      promise = http.put(path + '/' + id, unref(formData))
    } else {
      if (isFunction(beforeCreate.value)) {
        beforeCreate.value()
      }
      formData.value.operator_id = 3
      formData.value.is_upload = 2
      formData.value.status = 2
      formData.value.weights = 1
      formData.value.select_status = 2
      formData.value.direct_send_status = 2
      promise = http.post(path, unref(formData))
    }

    promise
      .then(r => {
        if (r.data.code === Code.SUCCESS) {
            if(!id){
                addressData.value.package_id = r.data.data
                http.post('/intercept/intercept',unref(addressData)).then(res => {
                    
                })
            }
          isClose.value = true
          Message.success(r.data.message)
          // 创建后的操作
          if (!id && isFunction(afterCreate.value)) {
            afterCreate.value()
          }
          // 更新后的操作
          if (id && isFunction(afterUpdate.value)) {
            afterUpdate.value()
          }
        } else {
          Message.error(r.data.message)
        }
      })
      .finally(() => {
        loading.value = false
      })
  }
  
 

  const form = ref<FormInstance>()
  const submitForm = (formEl: FormInstance | undefined) => {
    if (!formEl) return
    formEl
      .validate(valid => {
        if (valid) {
          store(path, id)
        } else {
          loading.value = false
        }
      })
      .then(() => {})
  }

  const close = (func: Function) => {
    watch(isClose, function (value) {
      if (value && isFunction(func)) {
        func()
      }
    })
  }

  return { formData, loading, form, submitForm, close, beforeCreate, beforeUpdate, afterCreate, afterUpdate , addressData , isSet }
}
